1242B - 0-1 MST - CodeForces Solution


dfs and similar dsu graphs sortings *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define speed                     \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define setp(x) fixed << setprecision(x)
#define endl "\n"
#define mod 1000000007
#define mod1 998244353
#define ff first
#define ss second
#define MAX 500005
#define N 100005
#define INF 1000000009
#define all(v) v.begin(), v.end()
#define sbit(a) __builtin_popcount(a)
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> ppl;
typedef map<ll, ll> mpll;
typedef map<vector<ll>, ll> mpvl;
ll power(ll x, ll y)
{
    ll res = 1;

    x = x;

    if (x == 0)
        return 0;
    while (y > 0)
    {

        if (y & 1)
            res = (res * x);

        y = y >> 1;
        x = (x * x);
    }

    return res;
}
set<ll> adj[N];
set<ll> se;
void dfs(ll v)
{
    for (auto it = se.begin(); it != se.end();)
    {
        if (adj[v].find(*it) == adj[v].end())
        {
            ll nxt = *it;
            se.erase(nxt); //atmost n times it will come here
            dfs(nxt);
            it = se.lower_bound(nxt); //take lower_bound//do lower_bound not it++ because meory locations may change
        }
        else
        {
            it++; //at most m times it will come here
        }
    }
}
void solve()
{
    ll n, m;
    cin >> n >> m;
    for (ll i = 1; i <= m; i++)
    {
        ll x, y;
        cin >> x >> y;
        adj[x].insert(y);
        adj[y].insert(x);
    }
    for (ll i = 1; i <= n; i++)
        se.insert(i);
    ll zerocomponents = 0;
    while (se.size())
    {
        zerocomponents++;
        ll v = *se.begin();
		se.erase(se.begin());
        dfs(v);
    }
    cout << zerocomponents - 1 << endl;
    return;
}
int main()
{

    speed;
    ll kk;
    kk = 1;
    // cin >> kk;
    while (kk--)
    {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash